﻿function getDate(x) {
    return new Date(Date.parse(x, "mm-dd-yyyy / h:MM tt"));
}

function gotoCommentPage(id, page) {
    $("#loaderIndicator").show();
    $.ajax(
        {
            type: "POST",
            url: "/Comment/CommentListPartial",
            data: { songId: id, currentPage: page },
            success: function (result) {
                $("#commentListAjax").html(result);
                $("#loaderIndicator").hide();
            },
            error: function (error) {
                alert(error);
            }
        });
}

function jumpToPage(pageElement, songId) {
    var pageNumber = $(pageElement).val();
    gotoCommentPage(songId, pageNumber);
}

function postComment(id) {
    $("#errorMessage").html("");
    var obj = $("#newCommentArea").val();
    if (obj.length < 10) {
        $("#errorMessage").html("Comment can't be less than 10 characters.");
        return;
    }
    $("#loaderIndicator").show();
    $(".buttonClass").hide(100);
    $.ajax(
        {
            type: "POST",
            url: "/Comment/CreateCommentPartial",
            data: { songId: id, newComment: obj },
            success: function (result) {
                $("#commentListAjax").html(result);
                $("#loaderIndicator").hide();
                $("newCommentArea").val("");
                $(".buttonClass").show(100);
            },
            error: function (error) {
                alert(error);
                $(".buttonClass").show(100);
            }
        });
}

function editComment(id) {
    $("#commentItemContent-" + id).hide(300);
    $.ajax(
        {
            type: "POST",
            url: "/Comment/UpdateComment",
            data: { commentId: id, newComment: $("#commentContent-" + id).val() },
            success: function (result) {
                $("#commentItemContent-" + id).show(300);
                if (!result.Result) {
                    $("#messageId-" + id).html(result.Message);
                }
                else {
                    $("#messageId-" + id).html("<img src='/Content/Images/require.png' />");
                }
            },
            error: function (error) {
                alert(error);
                $("#commentItemContent-" + id).show(300);
            }
        });
}

function reportComment(id) {
    $("#commentItemContent-" + id).hide(300);
    $.ajax(
        {
            type: "POST",
            url: "/Comment/ReportCommentFromCommunity",
            data: { commentId : id },
            success: function (result) {
                $("#commentItemContent-" + id).show(300);
                if (!result.Result) {
                    $("#messageId-" + id).html(result.Message);
                }
                else {
                    $("#messageId-" + id).html("<img src='/Content/Images/require.png' />");
                }
            },
            error: function (error) {
                alert(error);
                $("#commentItemContent-" + id).show(300);
            }
        });
}